#define _SHARED_FUNC
#include "pasting.h"

dcclist *
rm_dcc (dcclist * tete, dcclist * one)
{
  dcclist *ret = tete;
  if ((!one) || (!tete))
    return tete;

  if (one->next)		/* n'est pas le dernier maillon */
    one->next->prev = one->prev;

  if (one->prev)		 /* n'est pas le premier maillon */
    one->prev->next = one->next;
  else
    ret = one->next;

  free (one->nick);
  if (one->sock != -1)
    closesocket (one->sock);
  free (one);
  
  pconf.nb_socket--;
  return (ret);
}


dcclist *
get_dcc_by_socket (dcclist * tete, int sok)
{
  dcclist *runner;
  for (runner = tete; runner; runner = runner->next)
    if (runner->sock == sok)
      return runner;
  return NULL;
}

dcclist *
get_dcc_by_port (dcclist * tete, int port)
{
  dcclist *runner;
  for (runner = tete; runner; runner = runner->next)
    if (runner->port == port)
      return runner;
  return NULL;
}

byte
del_one (void)
{
  if (!dcc)
    return BAD;
  dcc = rm_dcc (dcc, dcc);
  return GOOD;
}


dcclist *
add_dcc (dcclist * tete, msg * i)
{
  dcclist *nd;
  char *len_tmp;

  nd = (dcclist *) malloc (sizeof (dcclist));
  nd->next = tete;
  nd->prev = NULL;
  if (tete)
    tete->prev = nd;
  
  nd->type = UNKNOW;
  nd->sock = -1;
  nd->current_len = 0;
  nd->create_time = time (NULL);
  
  int_to_ip (atoll (i->argu[3]), nd->ip);
  nd->port = atoi (i->argu[4]);

  nd->nick = (char *) malloc (strlen (i->sender_nick) + 1);
  strcpy (nd->nick, i->sender_nick);

  len_tmp = (char *) malloc (strlen (i->argu[5]) + 1);
  strcpy (len_tmp, i->argu[5]);
  len_tmp[strlen (len_tmp) - 1] = 0;
  nd->len = atoi (len_tmp);

  sprintf (nd->filename, "%u%u.txt", rand (), rand ());
  
  pconf.nb_socket++;
  free (len_tmp);

  return nd;
}
